package com.hackaton.ui;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.hackaton.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created with IntelliJ IDEA.
 * User: magiman
 * Date: 10/20/13
 * Time: 1:14 AM
 * To change this template use File | Settings | File Templates.
 */
public class SplashActivity extends Activity {

    private final int secTime = 4;
    private int num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_splash);

        getActionBar().hide();
        initLoadingTimer();
        initCloseTimer();
    }

    private void initLoadingTimer() {
        Timer timer = new Timer();
        final int[] res = new int[3];
        res[0] = R.drawable.ico_splash_tram;
        res[1] = R.drawable.ico_splash_trol;
        res[2] = R.drawable.ico_splash_taxi;
        TimerTask timerTask = new TimerTask() {

            @Override
            public void run() {
                SplashActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        if(SplashActivity.this.num==3) {
                            cancel();
                            return;
                        }
                        LinearLayout layout = (LinearLayout) findViewById(R.id.placeholder_splash);
                        ImageView imageView = new ImageView(SplashActivity.this);
                        imageView.setImageResource(res[SplashActivity.this.num++]);
                        layout.addView(imageView);
                    }
                });
            }
        };
        timer.schedule (timerTask, 500,700);
    }

    private void initCloseTimer() {
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                SplashActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        finish();
                    }
                });
            }
        };
        long delay = secTime * 1000;
        timer.schedule(timerTask, delay);
    }
}
